package com.freemyleft.left.left_app.mian.index.my.MyRanking;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.R2;
import com.freemyleft.left.left_app.mian.index.my.MyRanking.adapter.MyRankingAdapter;
import com.freemyleft.left.left_app.mian.index.my.MyRanking.adapter.MyRankingConvert;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.freemyleft.left.zapp.net.RestClient;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.ui.recycler.MultipleItemEntity;
import com.freemyleft.left.zapp.util.LeftPreference;
import com.google.gson.JsonObject;

import java.util.ArrayList;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.freemyleft.left.left_app.R2.id.tv_name;

/**
 * 我的排名
 */
public class MyRankingDelegate extends LeftDelegate {
    @BindView(R2.id.recy)
    RecyclerView recyclerView;
    @BindView(R2.id.tv_ranking)
    TextView tv_ranking;
    @BindView(R2.id.tv_my_ranking)
    TextView tv_my_ranking;
    @BindView(R2.id.tv_ranking_score)
    TextView tv_ranking_score;
    @BindView(R2.id.photo_imageCircle)
    CircleImageView photo;
    @BindView(tv_name)
    TextView name;
    @BindView(R2.id.tv_score)
    TextView tv_score;


    @Override
    public Object setLayout() {
        return R.layout.delegate_my_ranking;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        intiData();
    }

    private void intiData() {
        String token = LeftPreference.getCustomAppProfile(TOKEN);
        String teacherid = LeftPreference.getCustomAppProfile(TEACHER_ID);
        String cityid = LeftPreference.getCustomAppProfile("cityid");
        RestClient.builder()
                .loader(getContext())
                .url("/home/teacher/teacher_ranking")
                .params("cityid", cityid)
                .params("teacherid", teacherid)
                .params("token", token)
                .params("page", "1")
                .params("limitnum", "1")
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        Log.e("sss", responnse);
                        JSONObject result = JSONObject.parseObject(responnse);
                        JSONObject result1=result.getJSONObject("result");
                        JSONObject my = result1.getJSONObject("my");
                        String id = my.getString("id");
                        String ranking = my.getString("ranking");
                        tv_ranking.setText(ranking);
                        String head = my.getString("head");
                        Glide.with(getContext()).load(head).into(photo);
                        String cardname = my.getString("cardname");
                        name.setText(cardname);
                        String credit = my.getString("credit");
                        tv_score.setText(credit);
                        String lack = my.getString("lack");
                        tv_my_ranking.setText("距离推荐名师还差"+lack+"名");
                        ArrayList<MultipleItemEntity> data =
                                new MyRankingConvert().setJsonData(responnse).convert();
                        if (data != null) {
                            MyRankingAdapter adapter = new MyRankingAdapter(data);
                            LinearLayoutManager manager = new LinearLayoutManager(getContext());
                            manager.setOrientation(LinearLayoutManager.VERTICAL);
                            recyclerView.setAdapter(adapter);
                            recyclerView.setLayoutManager(manager);

                        } else {
                            recyclerView.setVisibility(View.GONE);
                        }

                    }
                }).buid().post();
    }

}

